﻿using UnityEngine;
using System.Collections;

public partial class LoginScene : Scene
{
	public enum LoginMode
	{
		Login,
		Join,
	}
	public LoginMode currentMode = LoginMode.Login;

	public enum LanguageType
	{
		Korean,
		English,
		Chinese,
		German,
		Russian,
	}

	public class LanguageSet
	{
		public Language type;
		public string name;
		public int lineSpacing;

		public LanguageSet(Language _type, string _name, int _lineSpacing)
		{
			type = _type;
			name = _name;
			lineSpacing = _lineSpacing;
		}
	}
	LanguageSet[] languageSet = new LanguageSet[5];
	LanguageSet[] dropDownLanguageSet = new LanguageSet[4];
	LanguageSet currentLanguageSet;

	int selectedDropBoxIndex = -1;
	bool isDropDownOpen = false;

	const int LIMIT_ID_LENGTH = 20;
	const int LIMIT_NAME_LENGTH = 10;

	string initialLoginIdText;
	string initialJoinIdText;
	string initialJoinNameText;
	
	public UILabel dropDownSelectedLabel;
	public UILabel[] dropDownListLabel;

	public Animation loginAnimation;
	public Animation joinAnimation;
	public Animation dropDownAnimation;

	public UIInput loginIdInput;
	public UIInput joinIdInput;
	public UIInput joinNameInput;

	public UILabel versionLabel;

	public bool isLoginAreaUp = false;

	#region G-Star Temp Code
	GameObject loginObject;
	GameObject gstarObject;
	#endregion

	public void OnClickDropDownItem(GameObject item)
	{
		if (dropDownAnimation.isPlaying)
			return;

		int selectedIdx = int.Parse(item.name) - 2;
		Debug.Log (selectedIdx);

		LanguageSet temp = dropDownLanguageSet[selectedIdx];
		dropDownSelectedLabel.text = temp.name;
		dropDownSelectedLabel.spacingX = temp.lineSpacing;

		dropDownLanguageSet[selectedIdx] = currentLanguageSet;
		dropDownListLabel[selectedIdx].text = currentLanguageSet.name;
		dropDownListLabel[selectedIdx].spacingX = currentLanguageSet.lineSpacing;

		currentLanguageSet = temp;
		UserData.Instance.m_Language = currentLanguageSet.type;

		ToggleDropDownList();
	}

	public void ToggleDropDownList()
	{
		if (dropDownAnimation.isPlaying)
			return;

		if (isDropDownOpen)
		{
			dropDownAnimation.Play("sort_close_login");
			isDropDownOpen = false;
		}
		else
		{
			dropDownAnimation.Play("sort_open_login");
			isDropDownOpen = true;
		}
	}

	void InitDropDownList()
	{
		if (Localization.language == null)
			return;

		languageSet[0] = new LanguageSet(Language.text_ko,		"한국어",		-1);
		languageSet[1] = new LanguageSet(Language.text_EN,	"English",	-1);
		languageSet[2] = new LanguageSet(Language.text_CN,	"中文",		-1);
		languageSet[3] = new LanguageSet(Language.text_DE,		"Deutsch",	-1);
		languageSet[4] = new LanguageSet(Language.text_RU,		"Русский",	-15);

		int idx = 0;

		foreach (LanguageSet ls in languageSet)
		{
			if (ls.type.ToString().Equals(Localization.language))
			{
				dropDownSelectedLabel.text = ls.name;
				currentLanguageSet = ls;
			}
			else
			{
				dropDownLanguageSet[idx] = ls;
				dropDownListLabel[idx].text = dropDownLanguageSet[idx].name;
				dropDownListLabel[idx].spacingX = dropDownLanguageSet[idx].lineSpacing;
				idx++;
			}
		}
	}

	public override void AwakeScene ()
	{
		#region G-Star Temp Code
		loginObject = GameObject.Find("Panel - Login");
		gstarObject = GameObject.Find("Panel - GStar");
		loginObject.transform.localPosition = new Vector3(9999.0f, 0.0f, 0.0f);
		gstarObject.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
		#endregion

		EventBinding();
		UserData.Instance.isFirstTimeMainScene = true;
		dropDownAnimation.Play("sort_close_login");
//
//		dropDownAnimation["sort_open_login"].time = 0.0f;
//		dropDownAnimation["sort_open_login"].enabled = true;
//		dropDownAnimation.Sample();
//		dropDownAnimation["sort_open_login"].enabled = false;

		InitDropDownList();
	}

	public override void StartScene ()
	{
		InitVariables();
		loginAnimation.Play("Login_login_in");
		//dropDownAnimation.Play("sort_close_login");
	}

	void InitVariables()
	{
		initialLoginIdText = loginIdInput.label.text = Localization.Get("uiStartCaution3");
		initialJoinIdText = joinIdInput.label.text = Localization.Get("uiStartCaution4"); 
		initialJoinNameText = joinNameInput.label.text = Localization.Get("uiStartCaution1");
		versionLabel.text = VerData.Ver;
	}

	void EventBinding()
	{
		//TODO:temp (multi-language test)
		/*
		GameObject.Find("TEMP_BT_LANG_KO").GetComponent<UIButton>().onClick.Add(new EventDelegate(() => {
			Debug.Log ("TEMP_BT_LANG_KO");
			UserData.Instance.m_Language = Language.text_ko;
			InitVariables();
		}));

        GameObject.Find("TEMP_BT_LANG_EN").GetComponent<UIButton>().onClick.Add(new EventDelegate(() =>
        {
            Debug.Log("TEMP_BT_LANG_EN");
            UserData.Instance.m_Language = Language.text_EN;
            InitVariables();
        }));

        GameObject.Find("TEMP_BT_LANG_CN").GetComponent<UIButton>().onClick.Add(new EventDelegate(() =>
        {
			Debug.Log ("TEMP_BT_LANG_CN");
			UserData.Instance.m_Language = Language.text_CN;
			InitVariables();
		}));

        GameObject.Find("TEMP_BT_LANG_DE").GetComponent<UIButton>().onClick.Add(new EventDelegate(() =>
        {
            Debug.Log("TEMP_BT_LANG_DE");
            UserData.Instance.m_Language = Language.text_DE;
            InitVariables();
        }));

        GameObject.Find("TEMP_BT_LANG_RU").GetComponent<UIButton>().onClick.Add(new EventDelegate(() =>
        {
            Debug.Log("TEMP_BT_LANG_RU");
            UserData.Instance.m_Language = Language.text_RU;
            InitVariables();
        }));
		*/

		#region G-Star Temp Code
		GameObject.Find("TEMP_BT_GSTAR").GetComponent<UIButton>().onClick.Add(new EventDelegate(() =>
		{
			if (gstarObject.transform.localPosition.x == 0.0f)
			{
				loginObject.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
				gstarObject.transform.localPosition = new Vector3(9999.0f, 0.0f, 0.0f);
			}
			else
			{
				loginObject.transform.localPosition = new Vector3(9999.0f, 0.0f, 0.0f);
				gstarObject.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
			}
		}));
		#endregion
	}
}
